package com.niceweather.android.logic.dao

import android.content.Context
import androidx.core.content.edit
import com.google.gson.Gson
import com.niceweather.android.base.BaseApplication
import com.niceweather.android.logic.model.Place

/**
 *
 * Created by callhh on 2020/6/21
 * SharedPreferences存储
 */
object PlaceDao {

    private  fun sharedPreferences() = BaseApplication.context.getSharedPreferences("nice_weather",
        Context.MODE_PRIVATE)

    /**
     * 判断数据是否被存储
     */
    fun isPlaceSaved() = sharedPreferences().contains("place")

    fun savePlace(place:Place){
        sharedPreferences().edit{
            putString("place",Gson().toJson(place))
        }
    }

    fun getSavedPlace():Place{
        val placeJson = sharedPreferences().getString("place", "")
        return Gson().fromJson(placeJson,Place::class.java )
    }

}